<?php 
/**
 * NewsNote Abstraction
 * 
 * @author MF
 * @version 2.0
 */
class NewsNote extends A_Content {
	
	/**
	 * Возвращает объект новостной записи
	 *
	 * @param integer $id
	 * @return SingleNewsNote
	 */
	public function getById($id) {
		if (!isset($this->_cashe_obj[$id])) {
			$this->_cashe_obj[$id] = new SingleNewsNote($this->_top,$this,$id);
		}
		
		return $this->_cashe_obj[$id];
	}
	
	public function add() {
		throw $this->_top->Utils->PushError('Adding SingleNewsNote object is making only threw SingleNews::add()','Системная ошибка. Обратитесь к разработчикам');
	}
}

class SingleNewsNote extends A_SingleContent {
	
	/**
	 * Returns poiner to parent SingleNews object
	 *
	 * @var SingleNews
	 */
	public $list;
	
	private $_extra_data_map;
	private $_extra_data_fields = array();
	
	public function loader()
	{
		unset($this->list);
		
		$get_data = $this->_top->_sql->exe("SELECT * FROM {$this->_top->_sql->tab_pref}news_note c WHERE news_note_id={$this->_content_id}");
		if (count($get_data)==0)
			throw $this->_top->Utils->PushError("NewsNote object Id: {$this->_content_id} not found", 'Системная ошибка. Обратитесь к разработчикам');
		else {
			$this->_data = $get_data[0];
		}
		
		$cfg = $this->list->config;
		for ($c=0;$c<count($cfg);$c++) {
			$this->_extra_data_map[$cfg[$c][0]] = $cfg[$c];
			array_push($this->_extra_data_fields,$cfg[1]);
		}
		
		return true;
	}
	
	public function __get($param) {
		if (isset($this->_data[$param])) {
			return $this->_data[$param];
		} elseif ($param=='list') {
			return $this->_top->News->getById($this->news_id);
		} elseif (isset($this->_extra_data_map[$param])) {
			return $this->_top->Content($this->_extra_data_map[$param][2])->getById($this->__get($this->_extra_data_map[$param][1]));
		} else {
			throw $this->_top->Utils->PushError('Geting Undefinded NewsNote object property','Системная ошибка. Обратитесь к разработчикам');
		}
		
	}
	
	public function __set($param,$value) {
		if ($param=='news_note_id' || $param=='news_id' || !isset($this->_data[$param]) || in_array($param,$this->_extra_data_fields)) {
			throw $this->_top->Utils->PushError('Setting Undefinded or Resticted NewsNote object property','Системная ошибка. Обратитесь к разработчикам');
		} else {
			$this->_data[$param] = $value;
			$ins_value = $this->_top->Utils->prepareValueForSQL($value);
			$this->_top->_sql->exeDummy("UPDATE {$this->_top->_sql->tab_pref}news_note SET `$param`='$ins_value' WHERE news_note_id={$this->_content_id}");
		}
	}
	
	protected function deleter() {
		$cfg = $this->list->config;
		for ($c=0;$c<count($cfg);$c++) {
			$this->_top->Content($cfg[$c][2])->getById($this->__get($cfg[$c][1]))->delete();
		}
		$this->_top->_sql->exeDummy("DELETE FROM {$this->_top->_sql->tab_pref}news_note WHERE news_note_id={$this->_content_id}");
	}
	
}
?>